<?php

	// +----------------------------------------------------------------------
	// | 站内消息
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\controller\ucenter\admin;

	use app\controller\admin\Common;
	use yicmf\builder\Edit;
	use yicmf\builder\Table;
	use app\event\ucenter\Notice as NoticeEvent;
	use app\model\ucenter\Feedback as FeedbackModel;
	use app\event\ucenter\Feedback as FeedbackEvent;

	class  Feedback extends Common
	{
		protected function initialize()
		{
			parent::initialize();
		}


		public function _filter()
		{
			$map = [];
			$map[] = ['is_end', '=', 0];
			$map[] = ['status', '=', 1];
//			$map[] = ['title', 'like', '%补单%'];
			return $map;
		}

		public function index()
		{
			// 读取规则列表
			// 显示页面
			$statusOptions = [
				[
					'id' => 1,
					'value' => '已读',
				],
				[
					'id' => 2,
					'value' => '未读',
				]
			];
			// 显示页面
			$builder = new Table();
			$builder->model(FeedbackModel::class)
				->where($this->_filter())
				->order('id DESC')
//                ->setSearchPostUrl('ucenter/admin/Feedback/index', '')
//				->buttonUpdate('ucenter/admin/Feedback/add')
				->searchTextLike('content', '反馈内容：')
				->searchSelect('status', '状态：', $statusOptions)
				->keyId()
				->keyTitle()
				->keyText('content', '内容',false,300)
//				->keyImagesModel('images','图片')
//				->keySwitch('is_finish', '回复', '已回|未回')
				->keyUser('user_id', '提交人')
				->keyIp()
                ->keyTime('reply_time','回复时间')
				->keyCreateTime()
				// ->actionView('read?id={$id}','查看')
					->actionUpdate('update?id={$id}','回复')
				->actionDelete();
			return $builder->fetch();


		}


		/**
		 * 新增消息
		 */
		public function update()
		{
			if ($this->request->isPost()) {
				$event = new FeedbackEvent();
				$result = $event->reply($this->request->post(), $this->user);
				if (0 == $result['code']) {
					$this->_returnSuccess( $result['message']);
				} else {
					$this->_returnError( '发送消息失败，失败信息【' . $result['message'] . '】');
				}
			} else {
				$data = FeedbackModel::where('status', 1)->where('id', $this->request->param('id'))->find();
				if (!$data)
				{
					$this->_returnError( '不存在');
				}
				$builder = new Edit();

				$builder->title('回复意见反馈')
					->keyTextInline('title', '标题')
					->keyTextArea('content', '内容')
					->keyImageShowMultiple('images','图片')
//					->keyMobile('mobile', '手机号')
					->keyTextArea('reply', '回复')
					->data($data)
					->buttonSubmit();
				return $builder->fetch();
			}
		}


		/**
		 * 删除消息
		 */
		public function delete()
		{
			$message = FeedbackModel::where('id', $this->request->param('id'))->find();
			if ($message) {
				$message['status'] = -2;
				$message->save();
				$this->_returnSuccess( '删除成功');
			} else {
				$this->_returnSuccess( '操作失败');
			}
		}


	}
